//
//  HomeHeartRateCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/27.
//

import UIKit
import MYModule
import RxSwift
import RxCocoa

class HomeHeartRateCell: HomeBaseCell {
    
    var viewModel: HomeHeartRateViewModel?
    
    override func makeUI() {
        super.makeUI()
        
        self.iconImagView.image = UIImage(named: "ic_home_today_heart_rate")
        self.titleLabel.text = "heart_rate_title".language
        
        workoutsTypeView.titleLabel.text = "heart_rate_workouts".language
        workoutsTypeView.icon.backgroundColor = UIColor.GPSTraining.theme
        sleepingTypeView.titleLabel.text = "heart_rate_sleeping".language
        sleepingTypeView.icon.backgroundColor = UIColor.Sleep.theme
        dayTimeTypeView.titleLabel.text = "heart_rate_day_time".language
        dayTimeTypeView.icon.backgroundColor = UIColor.HeartRate.theme
        
        
        measureButton.rx.tap.subscribe(onNext: {[weak self] in
            
            BluetoothResponder.shared.willMeasurement {
                
                if let isSelected = self?.measureButton.isSelected {
                    self?.measureButton.isSelected = !isSelected
                }
                self?.viewModel?.automaticMeasure()
                
            }
        }).disposed(by: measureButton.rx.disposeBag)
        
 
        bgView.addSubview(currentNumLabel)
        bgView.addSubview(unitLabel)
        bgView.addSubview(avgHeartRateTitleLabel)
        bgView.addSubview(chartView)
        bgView.addSubview(emptyView)
        bgView.addSubview(typeStackView)
        bgView.addSubview(measureButton)
        
        unitLabel.snp.makeConstraints { make in
            make.right.equalTo(bgView.snp.right).offset(-15)
            make.bottom.equalTo(currentNumLabel.snp.bottom).offset(-2)
        }
        
        currentNumLabel.snp.makeConstraints { make in
            make.bottom.equalTo(titleLabel.snp.bottom)
            make.right.equalTo(unitLabel.snp.left)
        }
        
        avgHeartRateTitleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(dateLabel.snp.centerY)
            make.trailing.equalTo(unitLabel.snp.trailing)
        }
        
       
        chartView.snp.makeConstraints { make in
            
            make.top.equalTo(dateLabel.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-16)
            
        }
        
        emptyView.snp.makeConstraints { make in
            make.centerX.equalTo(chartView.snp.centerX)
            make.centerY.equalTo(chartView.snp.centerY).offset(-10)
        }
        
        typeStackView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(chartView.snp.bottom).offset(10)
        }
        
        measureButton.snp.makeConstraints { make in
            make.top.equalTo(typeStackView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(15)
            make.height.equalTo(52)
            make.bottom.equalToSuperview().offset(bottom)
        }
    }
    
  
    func bindButton(measureState: UIControl.State) {
        
        if measureState == .selected {
            
            measureButton.isEnabled = true
            measureButton.isSelected = true
            
        } else if measureState == .normal {
            
            measureButton.isEnabled = true
            measureButton.isSelected = false
            
        } else if measureState == .disabled {
            
            measureButton.isEnabled = false
            measureButton.isSelected = false
        }
        
    }
    
    
    override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let viewModel = viewModel as? HomeHeartRateViewModel else {
            return
        }
        
        self.viewModel = viewModel
        
        let disposeBag = DisposeBag()
        viewModel.UIReplay.subscribe(onNext: {[weak self] in
            
            self?.dateLabel.text = $0.date
            
            self?.bindButton(measureState: $0.measureState)
            
            self?.currentNumLabel.text = $0.heartRateTitle

            self?.chartView.max = CGFloat($0.chartMaxData)
            self?.chartView.min = CGFloat($0.chartMinData)
            
            let titles = ["00:00",
                         "06:00",
                         "12:00",
                         "18:00",
                         "24:00"]
            self?.chartView.bind(barDatas: $0.chartDatas,
                                 horizontalTitles: titles,
                                 YAxisMaxValue: $0.chartMaxData,
                                 YAxisMinValue: $0.chartMinData,
                                 YAxisLineCount: $0.chartYAxisLineCount)
            
            self?.emptyView.isHidden = $0.hasData
            
        }).disposed(by: disposeBag)
        
        
        viewModel.hasDevicedReplay.subscribe(onNext: {[weak self] in
            
            self?.updateLayouts(hasDevice: $0)
            
        }).disposed(by: rx.disposeBag)
    
    }
    
    func updateLayouts(hasDevice: Bool?) {
        
        let hidden = hasDevice == true ? false : true
        
        measureButton.isHidden = hidden

        if !hidden {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(typeStackView.snp.bottom).offset(20)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(52)
                make.bottom.equalToSuperview().offset(-15)
                
            }
            
        } else {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(typeStackView.snp.bottom)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(0)
                make.bottom.equalToSuperview().offset(-15)
                
            }
        }
        
    }
    
    
    public lazy var emptyView: EmptyView = {
        
        let emptyView = EmptyView()
        emptyView.label.text = "heart_rate_no_information".language
        
        return emptyView
        
    }()
    
    
    private lazy var heartTypeStackView: StackView = {
        let heartTypeStackView = StackView()
        heartTypeStackView.alignment = .firstBaseline
        heartTypeStackView.axis = .horizontal
        heartTypeStackView.distribution = .equalCentering
       
        return heartTypeStackView
    }()
    
    public lazy var currentNumLabel: Label = {
        let currentNumLbl = Label()
        currentNumLbl.textAlignment = .right
        currentNumLbl.textColor = UIColor.HeartRate.theme
        currentNumLbl.font = Fonts.boldSystemFont(ofSize: 28)
        currentNumLbl.text = "97"
        return currentNumLbl
    }()

    public lazy var unitLabel: Label = {
        let untilLbl = Label()
        untilLbl.textAlignment = .right
        untilLbl.textColor = UIColor.assist_222222
        untilLbl.font = Fonts.systemFont(ofSize: 14)
        untilLbl.text = "BPM"
        return untilLbl
    }()

    public lazy var avgHeartRateTitleLabel: Label = {
        let rangeLbl = Label()
        rangeLbl.font = Fonts.systemFont(ofSize: 10)
        rangeLbl.textColor = UIColor.assist_D0D0D0
        rangeLbl.textAlignment = .right
        rangeLbl.text = "heart_rate_last_time".language
        return rangeLbl
    }()
    
    private lazy var chartView: HeartRateChartView = {
        let chartView = HeartRateChartView()
        return chartView
    }()
    
    private lazy var measureButton: HomeMeasureButton = {
        let measureButton = HomeMeasureButton()
        measureButton.setTitle("heart_rate_tap_to_measure_heart_rate".language, for: .normal)
        measureButton.setTitle("heart_rate_tap_to_end_heart_rate".language, for: .selected)
        
        let image = UIImage(named: "ic_home_today_heart_rate_measure")
        measureButton.setImage(image, for: .normal)
        
        let disableTitleColor = UIColor.assist_A9A9A9
        let disableImage = image?.originalImageWithColor(color: disableTitleColor)
        
        measureButton.setImage(disableImage, for: .disabled)
        measureButton.setTitleColor(disableTitleColor, for: .disabled)
        
        measureButton.setTitleColor(UIColor.assist_222222, for: .normal)
        measureButton.titleLabel?.font = Fonts.systemFont(ofSize: 14)
        measureButton.backgroundColor = UIColor.assist_F8F8F8
        measureButton.setImageTitleLayout(.imgLeft, spacing: 7)
        measureButton.cornerRadius = 14
        return measureButton
    }()
    
    private lazy var workoutsTypeView = SleepHeartTypeView()
    private lazy var sleepingTypeView = SleepHeartTypeView()
    private lazy var dayTimeTypeView = SleepHeartTypeView()
    
    private lazy var typeStackView: StackView = {
        
        let stackView = StackView()
        stackView.alignment = .center
        stackView.axis = .horizontal
        stackView.distribution = .equalCentering
        stackView.spacing = 70
        
//        stackView.addArrangedSubview(workoutsTypeView)
        stackView.addArrangedSubview(sleepingTypeView)
        stackView.addArrangedSubview(dayTimeTypeView)
        
        return stackView
        
    }()
    
}



